package com.liuya.safe.policy.controller;

import com.liuya.base.BaseController;
import com.liuya.common.WebUtil;
import com.liuya.safe.policy.service.QueryManagerService;
import com.liuya.safe.model.SafeQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 查询中转，
 */
@Controller
@Api(value = "查询策略", description = "查询策略")
@RequestMapping(value = "queryPolicy")
public class QueryPolicyController extends BaseController {

    @Autowired
    @Qualifier("queryManagerService")
    private QueryManagerService queryManagerService;

    @ApiOperation(value = "编辑查询策略")
//    @RequiresPermissions("sys.queryPolicy.editPolicy")
    @RequestMapping(value = "/editPolicy")
    public Object editPolicy(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        // edit policy
        // If a user clicks "policy?oper=...&id=..." url, it means user want to edit it for the first time.
        // So we need load query from database, not from session.
        // If a user want to continue edit query cached in session, should direct to url "designQuery?id=..." or "rawQuery?id=..."
        String id = WebUtil.getStringParameter(req, "id", "0");
        SafeQuery query = queryManagerService.getQuery(id);
        com.liuya.db.sql.Query sqlQuery = query.getSqlQuery();
        modelMap.addAttribute("id", id);
        if (sqlQuery.isRawSQL()) {//手工模式
            return "redirect:/queryRaw/editPolicy.do";
        } else {//设计器模式
            return "redirect:/queryDesign/editPolicy.do";
        }
    }

}
